const { BASEURL } = require('./baseurl')

export const fetch = (url = '', type = 'GET', data = {}) => {
    return new Promise((resolve, reject) => {
        wx.request({
            url: `${BASEURL}${url}`,
            data: data,
            method: type,
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                if (res.data.code == 200) {
                    resolve(res.data.data)
                } else {
                    wx.hideLoading()
                    // wx.showToast({
                    //     title: res.msg,
                    //     icon: 'none',
                    //     duration: 2000
                    // })
                    reject(res.data)
                }
            },
            fail: function(res) {
                // wx.showToast({
                //     title: res.msg,
                //     icon: 'none',
                //     duration: 2000
                // })
            }
        })
    })
}



export const newFetch = (url = '', type = 'GET', data = {}) => {
    return new Promise((resolve, reject) => {
        wx.request({
            url: `${BASEURL}${url}`,
            data: data,
            method: type,
            header: {
                'content-type': 'application/x-www-form-urlencoded'
            },
            success: function(res) {
                if (res) {
                    resolve(res.data)
                } else {
                    wx.hideLoading()
                    reject(res.data)
                }
            },
            fail: function(res) {
                
            }
        })
    })
}


export const filteremoji = (content) => {
    var ranges = [
    '\ud83c[\udf00-\udfff]',
    '\ud83d[\udc00-\ude4f]',
    '\ud83d[\ude80-\udeff]'
    ];
    emojireg = content .replace(new RegExp(ranges.join('|'), 'g'), '');
    return emojireg;
}



export const hasSensitiveWords = (str) => {
    if(str == '' || str == undefined) return false;
    var words = '台独,藏独,习近平,他妈的,操,色情,暴力,反动,贪腐,生奸内射,奸,操你妈,六合彩,落在赔率手,赌博，麻将,双色球,大乐透,逼,僧家,高价收购劝违章，道驾驶证分劝玄门专业';  // 常用敏感词，请自己添加
    var array = words.split(','), len = array.length;
    for (var i = 0; i < len; i++) {
      var item = array[i];
      if (str.indexOf(item) >= 0 && item != '') {
        return item;
      }
    }
    return false;
  }
  
  export const  replaceAll = (oldStr, reStr) => {
    var len = reStr.length, starStr = '';
    for (var i = 0; i < len; i++) {
      starStr += '*';
    }
    return oldStr.replace(new RegExp(reStr, "gm"), starStr);
  }
    
     
